<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="部门名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新增
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="部门名称"
        width="260"
      />
      <el-table-column
        prop="code"
        label="部门编码"
        width="120"
      />
      <el-table-column
        prop="leader"
        label="负责人"
        width="120"
      />
      <el-table-column
        prop="phone"
        label="联系电话"
        width="120"
      />
      <el-table-column
        prop="email"
        label="邮箱"
        width="200"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
      >
        <template #default="{row}">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import waves from '@/directive/waves'

export default {
  name: 'DepartmentManagement',
  directives: { waves },
  setup() {
    const list = ref(null)
    const listLoading = ref(true)
    const listQuery = reactive({
      name: undefined
    })

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取部门列表
      setTimeout(() => {
        list.value = []
        listLoading.value = false
      }, 1000)
    }

    const handleFilter = () => {
      getList()
    }

    const handleCreate = () => {
      // TODO: 实现新增部门功能
    }

    const handleEdit = (row) => {
      // TODO: 实现编辑部门功能
    }

    const handleDelete = (row) => {
      // TODO: 实现删除部门功能
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      listLoading,
      listQuery,
      handleFilter,
      handleCreate,
      handleEdit,
      handleDelete
    }
  }
}
</script>